﻿package fwk.tracking 
{
	

	import com.google.analytics.AnalyticsTracker; 
	import com.google.analytics.GATracker; 
	import flash.display.Sprite;
	import flash.display.Stage;
	import fwk.interfaces.IGoogleAnalytics;


	/** Exemple utilisation
	 * 
	 * 	 var xmlTracking = <data>

			<page id="home" pageUrl="homeTag.html" />
			<page id="test" pageUrl="pageTag.html" />

			<event id="eventTest" category="login" action="send" label="send login" value="" />
	
		</data>
	 * 
	 * 	_googleAnalytics = new GoogleAnalytics(stage, "UA-111-222", false  );
		_googleAnalytics.xmlTracking = _xmlTracking;
		_googleAnalytics.prefixe = "fr";
		
		// test
		_googleAnalytics.trackPageview("page"); // avec id du xml
		_googleAnalytics.trackPageview("videos/1.html"); // en passant directement le page url
		
		_googleAnalytics.trackEvent("eventTest");	// avec id du xml
		_googleAnalytics.trackEvent("videos", "play", "play video"); // avec les paramètres Event directement
	 * 
	 * 
	 * 

	 */
	public class GoogleAnalytics extends Sprite implements IGoogleAnalytics
	{
		private var _tracker:AnalyticsTracker;
		private var _xmlTracking:XML;
		
		private var _prefixe:String;
		

		
		public function GoogleAnalytics(stage:Stage, account:String = null, visualDebug:Boolean = false) 
		{
			
			stage.addChild(this);
			
			if ( account != null) {
				init(account, visualDebug);
			}
			
			
		}
		
		
		
		
		public function init(account:String, visualDebug:Boolean = false) 
		{
			_tracker = new GATracker( this, account, "AS3", visualDebug ); 
			
		}
		
		
		public function trackPageview(idOrPageUrl:String ):void 
		{
			if (  _tracker != null)
			{
				if ( _prefixe != null) {
					_tracker.trackPageview(_prefixe + "/" + getPageUrl(idOrPageUrl));
				}else {
					_tracker.trackPageview(_prefixe + "/" + getPageUrl(idOrPageUrl));
				}
				
				
			}else {
				trace("XXXX GoogleAnalytics not inititalized");
			}

		} 
		public function trackEvent(idOrCategory:String, action:String = null,  label:String = null, value:Number = NaN ):void 
		{
			if (  _tracker != null)
			{
				
				var nodeEvent:XML = getNode(idOrCategory);
				
				if ( nodeEvent != null)
				{
					if (  String(nodeEvent.@value) != "") {
						value = parseInt( String(nodeEvent.@value) );
					}
					if (  String(nodeEvent.@label) != "") {
						label = String(nodeEvent.@label);
					}
					if ( _prefixe != null) {
						_tracker.trackEvent(  _prefixe + "/" +  nodeEvent.@category, nodeEvent.@action, label, value );
					}else {
						_tracker.trackEvent(  nodeEvent.@category, nodeEvent.@action, label, value );
					}
					

				}else {
					
					if ( action != null) {
						if ( _prefixe != null) {
							_tracker.trackEvent(  _prefixe + "/" + idOrCategory, action, label, value );
						}else {
							_tracker.trackEvent(  idOrCategory, action, label, value );
						}
						
					}else {
						trace("XXXX trackEvent l'id " + idOrCategory + " n'a pas été trouvé dans le xml de tracking Google");
					}

				}

				
			}else {
				trace("XXXX GoogleAnalytics not inititalized");
			}

		} 
		private function getNode(id:String):XML
		{
			if (_xmlTracking != null)
			{
				
				var nodeList:XMLList = _xmlTracking.event.(@id == id);
				
				if (nodeList != null && nodeList.length() > 0) {
					return nodeList[0];
				}				
			}
			
			return null;
		}
		
			
		private function getPageUrl(id:String):String {
			
			if (_xmlTracking != null)
			{
				
				var nodeList:XMLList = _xmlTracking.page.(@id == id);
				
				if (nodeList != null && nodeList.length() > 0) {
					return nodeList[0].@pageUrl;
				}
				
				
			}
			
			return id;
			
		}
		
		
		public function get xmlTracking():XML { return _xmlTracking; }
		
		public function set xmlTracking(value:XML):void 
		{
			_xmlTracking = value;
		}
		
		public function get prefixe():String { return _prefixe; }
		
		public function set prefixe(value:String):void 
		{
			_prefixe = value;
		}


	}
	
}